#!/usr/bin/env bash

POMS_PATH=/opt/poms
POMS_INSTALL_PATH=/nas/video/source/upload

function deploy_service() {
    SERVICE_NAME=$1
    echo "停止poms-${SERVICE_NAME}"
    ${POMS_PATH}/${SERVICE_NAME}/bin/service.sh stop

    echo "备份poms-${SERVICE_NAME}"
    mkdir -p /opt/backup/`date +%Y%m%d`/poms

    mv -f ${POMS_PATH}/${SERVICE_NAME}/ /opt/backup/`date +%Y%m%d`/poms/${SERVICE_NAME}.`date +%H%M%S`

    echo "解压并启动poms-${SERVICE_NAME}服务"
    tar zxvf ${POMS_INSTALL_PATH}/poms/poms-${SERVICE_NAME}-1.0.0.tar.gz -C ${POMS_PATH}
    cd ${POMS_PATH}/${SERVICE_NAME}/bin
    ./service.sh start preview

    echo "完成部署poms-${SERVICE_NAME}"
}

SERVICE_LIST=("manage" "search" "package-price" "collect" "image" "output" "publish" "bc" "log" "setting")
hasService=0

checkEnvParam(){
  for cs_name in ${SERVICE_LIST[@]};do
    if [[ "${cs_name}" == "$1" ]] ; then
        hasService=1
    fi
  done
}


echo "开始部署POMS服务"

deploy_service_name=$1
if [[ -n "${deploy_service_name}" && "${deploy_service_name}" != "all" ]]; then
    deploy_service ${deploy_service_name}
elif [[ "${deploy_service_name}" == "all" ]]; then
    for s_name in ${SERVICE_LIST[@]};do
        deploy_service ${s_name}
    done
else
    echo "使用方法 : ./deploy_poms.sh all 或者 ./deploy_poms.sh ${SERVICE_LIST}"
fi

echo "完成POMS服务部署"
